//
//  TravelTactic_AreaViewController.m
//  Test
//
//  Created by Rain on 15/11/16.
//  Copyright © 2015年 Rain. All rights reserved.
//

#import "TravelTactic_AreaViewController.h"
#import "DataHandle.h"
#import "TravelTactic_AreaTableViewCell.h"
#import "TravelTactic_AreaModel.h"
#import "TravelTactic_RegionViewController.h"

@interface TravelTactic_AreaViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *modelArr;

@end

@implementation TravelTactic_AreaViewController

- (void)dealloc {
    [_modelArr release];
    [_tableView release];
    
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController.navigationBar setHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.modelArr = [NSMutableArray array];
    
    [self dataload_Area];
    
    [self createTableView_Area];
    
}

- (void)dataload_Area {
    
    [DataHandle contectWithURL:_urlStr andParameters:nil andViewController:self andBlock:^(id response) {
        for (NSDictionary *tempDic in response) {
            TravelTactic_AreaModel *model = [[TravelTactic_AreaModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.modelArr addObject:model];
            [model release];
        }
        [self.tableView reloadData];
        self.navigationItem.title = [NSString stringWithFormat:@"%@攻略", [_modelArr[0] name_zh_cn]];
    }];
    
}

- (void)createTableView_Area {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[TravelTactic_AreaTableViewCell class] forCellReuseIdentifier:@"areaCell"];
    
}

#pragma mark 返回单元格行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _modelArr.count;
}

#pragma mark 返回单元格数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TravelTactic_AreaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"areaCell" forIndexPath:indexPath];
    
    cell.travelTactic_AreaModel = _modelArr[indexPath.row];
    return cell;
}

#pragma mark 返回高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 230 * SWIDTH;
}

#pragma mark 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TravelTactic_RegionViewController *regionViewController = [[TravelTactic_RegionViewController alloc] init];
    regionViewController.urlStr = [NSString stringWithFormat:@"http://chanyouji.com/api/articles.json?destination_id=%@&page=1", [_modelArr[indexPath.row] ID]];
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.navigationController pushViewController:regionViewController animated:YES];
    [regionViewController release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
